package com.soa.ws2http.config;

import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.context.embedded.undertow.UndertowDeploymentInfoCustomizer;
import org.springframework.boot.context.embedded.undertow.UndertowEmbeddedServletContainerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component;

import io.undertow.servlet.api.SecurityConstraint;
import io.undertow.servlet.api.WebResourceCollection;

@Component
public class UndertowWebServerCustomizer {

    @Bean 
    public EmbeddedServletContainerCustomizer containerCustomizer() { 
     return new EmbeddedServletContainerCustomizer() { 
      @Override 
      public void customize(ConfigurableEmbeddedServletContainer container) { 
       if (container.getClass().isAssignableFrom(UndertowEmbeddedServletContainerFactory.class)) { 
        UndertowEmbeddedServletContainerFactory undertowContainer = (UndertowEmbeddedServletContainerFactory) container; 
        undertowContainer.addDeploymentInfoCustomizers(new ContextSecurityCustomizer()); 
       } 
      } 
     }; 
    } 

    private static class ContextSecurityCustomizer implements UndertowDeploymentInfoCustomizer { 
     @Override 
     public void customize(io.undertow.servlet.api.DeploymentInfo deploymentInfo) { 
      SecurityConstraint constraint = new SecurityConstraint(); 
      WebResourceCollection traceWebresource = new WebResourceCollection(); 
      traceWebresource.addUrlPattern("/*"); 
      traceWebresource.addHttpMethod(HttpMethod.TRACE.toString()); 
      constraint.addWebResourceCollection(traceWebresource); 
      deploymentInfo.addSecurityConstraint(constraint); 
     } 
    } 
}
